<?php


namespace app\api\controller;


use app\common\model\AdminAdmin;
use app\common\model\BusinessCarousel;
use app\common\model\BusinessChapter;
use app\common\model\BusinessCourse;
use app\common\model\BusinessOrder;

class Course
{
    /**
     * 获取轮播图
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getIndexInfo()
    {
        $banner = BusinessCarousel::select();
        $admin_id = input('param.admin_id');
        $admin = AdminAdmin::where('id', $admin_id)->find();
        $order = BusinessOrder::where('school_id', $admin['school_id'])
            ->where('end_time', '>', time())
            ->column('course_id');
        $course = BusinessCourse::with([
            'get_teacher' => function($a) {
                $a->field('id,teacher_name,teacher_avatar');
            }
        ])->field('id,course_name,course_image,teacher_id')
            ->where(['status' => 1, 'is_index' => 1])
            ->whereIn('id', $order)
            ->select();
        resReturn([
            'banner' =>$banner,
            'course' =>$course,
        ]);
    }

    /**
     * 获取全部课程
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllCourse()
    {
        $admin_id = input('param.admin_id');
        $admin = AdminAdmin::where('id', $admin_id)->find();
        $order = BusinessOrder::where('school_id', $admin['school_id']) ->where('end_time', '>', time())->column('course_id');
        $course = BusinessCourse::with([
            'get_teacher' => function($a) {
                $a->field('id,teacher_name,teacher_avatar');
            }
        ])->field('id,course_name,course_image,teacher_id')
            ->where(['status' => 1])
            ->whereIn('id', $order)
            ->select();
        resReturn($course);
    }

    /**
     * 课程详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function courseDetail()
    {
        $id = input('param.id');
        $course = BusinessCourse::with([
            'get_teacher' => function($a) {
                $a->field('id,teacher_name,teacher_avatar');
            }
        ])->where(['id' => $id])->find();
        $course['chapter'] = BusinessChapter::where('course_id', $id)->where('status', 1)->select();
        resReturn($course);
    }


}